MenuOpen = false
local RetrievedData2 = false
local SelectedClass = ""


local AllClasses = {

	"Shieldbearer",
	[[Shieldbearers use their quick wait to gain the 
	attention of enemies, and use their increased defensive 
	capabilities and sustained damage to steer the direction 
	of the fight.]],
	"Man At Arms",
	[[With unmatched skill in weaponry, the Man at 
	Arms can throw players back and deal quick and 
	devastating bursts of damage.]],
	"Brawler",
	[[Brawlers specialize in using fierce punches, kicks, and jabs 
	to take down opponents.]],
	"Mage",
	[[Mages channel the devastating power of the elements through 
	their weapon to unleash brutal attacks on their foes.]],
	"Blademaster",
	[[Blademasters make use of rapid, sweeping weapon strikes 
	and heavy bleed damage to overcome waves of enemies."]],
	"Ranger",
	[[Rangers use their keen sight and marksmanship skills 
	to riddle their foes with arrows.]],
}

local RegenTypes = {
	{"Rage", 
	[[A life of war and bloodshed has transformed you into a ruthless 
	berserker. Upon taking damage, you gain 1 rage per 1% of health lost. 
	For every 10 rage spent, 1% of your health is restored. In addition, your 
	filler attacks generate a moderate amount of rage.
	
	Tip: Rage is most effective when you're taking large amounts of damage. 
	If you're expecting to be the main target of your enemies, this is the 
	resource to choose.
	
	Good for: Sustained combat, solo survivability, self-healing.
	Bad for: Burst damage, group fights.]],
	"devin/blackbetty/spells/warrior/battlecry.png"
	},
	
	{"Energy", 
	[[Through intense combat training, you have exceptional endurance. Your 
	abilities feed off of your energy, which regenerates constantly. Your filler 
	attacks will also generate a small amount of energy.
	
	Tip: Energy is the single best resource for dealing burst damage. When
	using Energy, a starting value of 150 gives you the ability to begin 
	fights with devastating chain attacks. However, lower resource 
	regeneration makes this one of the worst tanking resource types.
	
	Good for: Beginners, burst damage.
	Bad for: Sustained combat]],
	"devin/blackbetty/spells/brawler/auto_1.png"
	},
				
	{"Mana", 
	[[Intense meditation has given you the ability to draw power directly from 
	the elements. Your physical and magical abilities will feed off of a small 
	but quickly regenerating source of power.
	
	Tip: As a Mana user, your advantage lies in your sustained damage. Use 
	your quickly recharging resources to barrage your opponents with 
	low-cost spells. While using mana, your filler attacks will not generate 
	resource.
	
	Good for: Constant damage, self-healing.
	Bad for: Burst damage.]],
	"devin/blackbetty/spells/warrior/thunderclap.png"
	},
	
	{"Vampirism", 
	[[An ability granted to you at birth allows you vampiric powers. Upon 
	dealing damage, you steal the power of your enemies. This resource can 
	be stored until spent by your abilities.
	
	Tip: Using the Vampirism resource allows you to benefit from a larger 
	resource pool. In addition, your filler attacks will generate a massive 
	amount of resources. This will allow you to build a large amount of
	power over time to plan devastating attacks against your foes.
	
	Good for: Huge built-up burst damage, moderate sustained damage.
	Bad for: On-demand burst]],
	"devin/blackbetty/spells/lifebinder/bloodfunnel.png"
	},
}

OwnedSpells = {}
OwnedClasses = {true, true, true, false}
OwnedItems = {}
OwnedCurrency = {}
local AllSlotTypeSpells = {}
local ClearSelected = false

local Char_Presets = {{"Default","Man At Arms",nil,nil,nil}}

local SelectedPreset = 0
local SpellToSwap = 0

local MAA_SpellTypes = {SPELLTYPE_MOBILITY, SPELLTYPE_MAA, SPELLTYPE_SLOW, SPELLTYPE_MELEEDAMAGE, SPELLTYPE_HYBRID}
local SB_SpellTypes = {SPELLTYPE_MOBILITY, SPELLTYPE_SHIELDDAMAGE, SPELLTYPE_MELEEDAMAGE, SPELLTYPE_SHIELDDEFENSIVE, SPELLTYPE_UTILITY}
local BRAWL_SpellTypes = {SPELLTYPE_MOBILITY, SPELLTYPE_MELEEDAMAGE, SPELLTYPE_MELEEDAMAGE, SPELLTYPE_HYBRID, SPELLTYPE_BRAWLER}
local MAGE_SpellTypes = {SPELLTYPE_MOBILITY, SPELLTYPE_BLOODMAGE, SPELLTYPE_MAGICDAMAGE, SPELLTYPE_HYBRID, SPELLTYPE_UTILITY}
local RNG_SpellTypes = {SPELLTYPE_MOBILITY, SPELLTYPE_RANGEDAMAGE, SPELLTYPE_RANGERUTILITY, SPELLTYPE_HYBRID, SPELLTYPE_UTILITY}
local BM_SpellTypes = {SPELLTYPE_MOBILITY, SPELLTYPE_BLADEMASTER, SPELLTYPE_BLADEMASTER2, SPELLTYPE_HYBRID, SPELLTYPE_SLOW}
local ClassFillers = {{"class_shieldbearer", 11003, 11013},
					{"class_maa", 1100, 1101},
					{"class_brawler", 1211, 1212},
					{"class_mage", 1110, 13011},
					{"class_blademaster", 13130, 13131},
					{"class_ranger", 13003, 11103}}
local UsableSpells = {{},{},{},{},{}}
local AllowedTypes = {}
local CurrentSetSpells = {}
local CurrentSetSpellImages = {}
local CurrentClassName = "class_default"
local CurrentEnergyRegen = 1
local SpellBoxes = {}



net.Receive("SendPlayerInventory2", function()
	print("RETRIEVED PLAYER DATA")
	local data = net.ReadTable()
	
	OwnedItems = util.JSONToTable(data[1])
	OwnedSpells = util.JSONToTable(data[2])
	OwnedClasses = util.JSONToTable(data[3])
	OwnedCurrency = util.JSONToTable(data[4])
	Char_Presets = util.JSONToTable(data[5])
	
	RetrievedData2 = true
	
end)

net.Receive("ShowPresetMenu", function()
	print("Wolololo")
	if(!MenuOpen)then
		ShowPresetWindow()
	end
end)

net.Receive("SendAllSpells", function() 
	print("received all spells")
	AllSpells = net.ReadTable()
end)

function RetrievePlayerData()
	RetrievedData2 = true
end

function ShowPresetWindow()
	
	if(!RetrievedData2)then
		net.Start("RetrievePlayerInventory2")
		net.SendToServer()
		surface.PlaySound("devin/blackbetty/menu/select_target.wav")
		local RetrievingMenu = vgui.Create("DFrame")
			RetrievingMenu:SetTitle( "..." )
			RetrievingMenu:SetPos( ScrW()/2, ScrH()/2 )
			RetrievingMenu:SetSize( 200, 100 )
			RetrievingMenu:SetDeleteOnClose(true)
			RetrievingMenu:ShowCloseButton(false)
			RetrievingMenu.Think = function() 
				if(RetrievedData2)then
					RetrievingMenu:Remove()
					ShowPresetWindow()
				end
			end
		
		local RetrievingMenuLabel = vgui.Create( "DLabel", RetrievingMenu)
			RetrievingMenuLabel:SetText( "Retrieving player data." )
			RetrievingMenuLabel:Center()
			RetrievingMenuLabel:SizeToContents()
		
	else
		surface.PlaySound("devin/blackbetty/menu/spellbook_open.wav")
		MenuOpen = true
		optionsMenu = vgui.Create("DFrame")
			optionsMenu:SetTitle( "Vendor" )
			optionsMenu:SetSize( 500, 800 )
			optionsMenu:SetDeleteOnClose(true)
			optionsMenu:ShowCloseButton(true)
			optionsMenu:Center()
			optionsMenu:SetMouseInputEnabled(true)
			optionsMenu:MakePopup()
			optionsMenu.Close = function() 
			RetrievedData = false 
			MenuOpen = false 
			UsableSpells = {{},{},{},{},{}}
			AllowedTypes = {}
			CurrentSetSpells = {}
			CurrentSetSpellImages = {}
			SpellBoxes = {}
			optionsMenu:Remove() 
			surface.PlaySound("devin/blackbetty/menu/spellbook_close.wav") end
			
		local BookPages = vgui.Create( "DPropertySheet", optionsMenu )
			BookPages:SetPos( 5, 30 )
			BookPages:SetSize( 490, 765-35 )
			BookPages:SetFadeTime(0)
			
		CharPresets = vgui.Create("DImage", optionsMenu)
			CharPresets:SetPos( 5, 30 )
			CharPresets:SetSize( 490, 765-35 )
			CharPresets:SetVisible(false)
			CharPresets:SetImage("devin/blackbetty/spellbook/background.png")
				
		CharInventory = vgui.Create("DImage", optionsMenu)
			CharInventory:SetPos( 0, 0 )
			CharInventory:SetSize( 490, 765-35 )
			CharInventory:SetVisible(false)
			CharInventory:SetImage("devin/blackbetty/spellbook/background.png")
				
		
		PresetList = vgui.Create( "DComboBox", CharPresets )
			PresetList:SetPos( 5, 5 )
			PresetList:SetSize( 100, 20 )
			PresetList:SetValue( "No presets." )
			for i = 1, #Char_Presets, 1 do
				print(#Char_Presets)
				if(Char_Presets[i][1] != "" or Char_Presets[i][1] != nil)then
					PresetList:AddChoice(Char_Presets[i][1])
				end
			end
			if(Char_Presets[1] != nil)then
				PresetList:SetValue( "Choose a preset." )
			else
				PresetList:SetValue( "No presets." )
			end
			PresetList.OnSelect = function( panel, index, value, data )
				UsableSpells = {{},{},{},{},{}}
				AllowedTypes = {}
				CurrentSetSpells = {}
				CurrentSetSpellImages = {}
				SpellBoxes = {}
				SelectedPreset = 0
				SpellToSwap = 0
				surface.PlaySound("devin/blackbetty/menu/select_target.wav")
				SelectedPreset = index
				MakePresetContent(CharPresets, index)
			end
			
		RemovePreset = vgui.Create( "DButton", CharPresets )
			RemovePreset:SetPos( 230, 5 )
			RemovePreset:SetText( "Remove Preset" )
			RemovePreset:SetSize( 100, 20 )
			RemovePreset.DoClick = function()
				if(#Char_Presets > 0)then
					surface.PlaySound("physics/plaster/ceilingtile_break2.wav")
					if(PresetList != nil)then
						PresetList:Remove()
					end
					RemovePresetContent()
					table.remove(Char_Presets, SelectedPreset)
					net.Start("UpdatePlayerPresets")
					net.WriteString(util.TableToJSON(Char_Presets))
					net.SendToServer()
					PresetList = vgui.Create( "DComboBox", CharPresets )
						PresetList:SetPos( 5, 5 )
						PresetList:SetSize( 100, 20 )
						PresetList:SetValue( "No presets." )
						for i = 1, #Char_Presets, 1 do
							print(#Char_Presets)
							if(Char_Presets[i][1] != "" or Char_Presets[i][1] != nil)then
								PresetList:AddChoice(Char_Presets[i][1])
							end
						end
						if(Char_Presets[1] != nil)then
							PresetList:SetValue( "Choose a preset." )
						else
							PresetList:SetValue( "No presets." )
						end
						PresetList.OnSelect = function( panel, index, value, data )
							UsableSpells = {{},{},{},{},{}}
							AllowedTypes = {}
							CurrentSetSpells = {}
							CurrentSetSpellImages = {}
							SpellBoxes = {}
							surface.PlaySound("devin/blackbetty/menu/select_target.wav")
							SelectedPreset = index
							MakePresetContent(CharPresets, index)
						end
					end
			end
		
		local CreatePreset = vgui.Create( "DButton", CharPresets )
			CreatePreset:SetPos( 120, 5 )
			CreatePreset:SetText( "Create Preset" )
			CreatePreset:SetSize( 100, 20 )
			CreatePreset.DoClick = function()
				surface.PlaySound("devin/blackbetty/menu/select_target.wav")
				
				local MakeCoverUp = vgui.Create( "DPanel", CharPresets )
					MakeCoverUp:SetPos( 0, 0 )
					MakeCoverUp:SetSize( 490, 765-35 )
					MakeCoverUp:SetBackgroundColor(Color(0, 0, 0, 235))
				
				local MakePresetWindowUnder = vgui.Create( "DPanel", CharPresets )
					MakePresetWindowUnder:SetPos( 0, 256 )
					MakePresetWindowUnder:SetSize( 490, 160 )
					MakePresetWindowUnder:SetBackgroundColor(Color(0, 0, 0, 110))
				
				local MakePresetPanel = vgui.Create( "DPanel", MakePresetWindowUnder )
					MakePresetPanel:SetPos( 10, 30 )
					MakePresetPanel:SetSize( 245, 55 )
					
				local SmallTextBkg = vgui.Create( "DPanel", MakePresetWindowUnder )
					SmallTextBkg:SetPos( 285, 30 )
					SmallTextBkg:SetSize( 150, 16 )
					
				local ChooseClassLabel = vgui.Create( "DLabel", MakePresetWindowUnder )
					ChooseClassLabel:SetPos( 300, 30 )
					ChooseClassLabel:SetText( "Select your preset's class:" )
					ChooseClassLabel:SizeToContents()
					ChooseClassLabel:SetDark( 1 )
					
				local SmallTextBkg2 = vgui.Create( "DPanel", MakePresetWindowUnder )
					SmallTextBkg2:SetPos( 285, 110 )
					SmallTextBkg2:SetSize( 150, 16 )
					
				local ChoosePresetNameLabel = vgui.Create( "DLabel", MakePresetWindowUnder )
					ChoosePresetNameLabel:SetPos( 300, 110 )
					ChoosePresetNameLabel:SetText( "Select your preset's name:" )
					ChoosePresetNameLabel:SizeToContents()
					ChoosePresetNameLabel:SetDark( 1 )
					
				local TextEntry = vgui.Create( "DTextEntry", MakePresetWindowUnder )              
					 TextEntry:SetPos( 285, 130 )                
					 TextEntry:SetSize( 150, 16 )          
					 TextEntry:SetText( "Preset" )
					
				local OutOfCreatePreset = vgui.Create( "DButton", MakePresetWindowUnder )
					OutOfCreatePreset:SetPos( 10, 120 )
					OutOfCreatePreset:SetText( "Cancel" )
					OutOfCreatePreset:SetSize( 100, 20 )
					OutOfCreatePreset.DoClick = function()
						MakePresetWindowUnder:Remove()
						MakeCoverUp:Remove()
						surface.PlaySound("devin/blackbetty/menu/select_target.wav")
					end
					
				local DoCreatePreset = vgui.Create( "DButton", MakePresetWindowUnder )
					DoCreatePreset:SetPos( 140, 120 )
					DoCreatePreset:SetText( "Create" )
					DoCreatePreset:SetSize( 100, 20 )
					DoCreatePreset.DoClick = function()
						surface.PlaySound("devin/blackbetty/menu/select_target.wav")
						if(SelectedClass != "")then
							print("oi")
							local temptable = 
							table.Add(Char_Presets, {{TextEntry:GetValue(), SelectedClass, nil, nil, nil}})
							net.Start("UpdatePlayerPresets")
							net.WriteString(util.TableToJSON(Char_Presets))
							net.SendToServer()
							if(PresetList != nil)then
								PresetList:Remove()
							end
							MakePresetWindowUnder:Remove()
							MakeCoverUp:Remove()
							PresetList = vgui.Create( "DComboBox", CharPresets )
								PresetList:SetPos( 5, 5 )
								PresetList:SetSize( 100, 20 )
								PresetList:SetValue( "Select a preset." )
								for i = 1, #Char_Presets, 1 do
									print(#Char_Presets)
									if(Char_Presets[i][1] != "" or Char_Presets[i][1] != nil)then
										PresetList:AddChoice(Char_Presets[i][1])
									end
								end
								PresetList:ChooseOptionID( #Char_Presets )
								MakePresetContent(CharPresets, #Char_Presets)
								PresetList.OnSelect = function( panel, index, value, data )
									UsableSpells = {{},{},{},{},{}}
									AllowedTypes = {}
									CurrentSetSpells = {}
									CurrentSetSpellImages = {}
									SpellBoxes = {}
									surface.PlaySound("devin/blackbetty/menu/select_target.wav")
									SelectedPreset = index
									MakePresetContent(CharPresets, index)
								end
							else
								surface.PlaySound("buttons/button10.wav")
							end
						end
				CreateClassDescText = vgui.Create( "DLabel", MakePresetPanel )
					CreateClassDescText:SetPos( 8, 5 )
					CreateClassDescText:SetSize( 245, 200 )
					CreateClassDescText:SetText( "Select a class ->" )
					CreateClassDescText:SizeToContentsY()
					CreateClassDescText:SetDark( 1 )
					
				local ChooseClassList = vgui.Create( "DComboBox", MakePresetWindowUnder )
					ChooseClassList:SetPos( 285, 50 )
					ChooseClassList:SetSize( 150, 16 )
					for i = 1, #AllClasses, 2 do
						if(AllClasses[i] != "" or AllClasses[i] != nil)then
							ChooseClassList:AddChoice(AllClasses[i])
						end
					end
					ChooseClassList.OnSelect = function( panel, index, value, data )
						surface.PlaySound("devin/blackbetty/menu/select_target.wav")
						if(CreateClassDescText != nil)then
							CreateClassDescText:Remove()
						end
						
						CreateClassDescText = vgui.Create( "DLabel", MakePresetPanel )
							CreateClassDescText:SetPos( 8, 5 )
							CreateClassDescText:SetSize( 245, 200 )
							CreateClassDescText:SetText( AllClasses[2*index] )
							CreateClassDescText:SizeToContentsY()
							CreateClassDescText:SetDark( 1 )
							
						SelectedClass = AllClasses[(index*2)-1]
					end

			end
		end
end

function MakePresetContent(parent, index)
	local classicon = ""
	
	if(Char_Presets[index][2] == "Man At Arms")then
		classicon = "devin/blackbetty/hudicons/maa.png"
		CurrentClassName = "class_maa"
	elseif(Char_Presets[index][2] == "Shieldbearer")then
		classicon = "devin/blackbetty/hudicons/sb.png"
		CurrentClassName = "class_shieldbearer"
	elseif(Char_Presets[index][2] == "Brawler")then
		classicon = "devin/blackbetty/spells/brawler/auto_1.png"
		CurrentClassName = "class_brawler"
	elseif(Char_Presets[index][2] == "Mage")then
		classicon = "devin/blackbetty/spells/lifebinder/bloodbolt.png"
		CurrentClassName = "class_mage"
	elseif(Char_Presets[index][2] == "Blademaster")then
		classicon = "devin/blackbetty/spells/blademaster/bladestorm.png"
		CurrentClassName = "class_blademaster"
	elseif(Char_Presets[index][2] == "Ranger")then
		classicon = "devin/blackbetty/spells/ranger/frostbarrage.png"
		CurrentClassName = "class_ranger"
	else
		classicon = "devin/blackbetty/hudicons/maa.png"
	end
	
	
	if(PresetContent != nil)then
		PresetContent:Remove()
	end
	PresetContent = vgui.Create("DPanel", parent)
		PresetContent:SetPos( 5, 30 )
		PresetContent:SetSize( 463, 660 )
		PresetContent:SetBackgroundColor(Color(80, 80, 80, 245))
		
	PresetNamePanel = vgui.Create("DPanel", PresetContent)
		PresetNamePanel:SetPos( 0, 0 )
		PresetNamePanel:SetSize( 463, 50 )
		PresetNamePanel:SetBackgroundColor(Color(0, 0, 0, 140))
		
	PresetContentName = vgui.Create( "DLabel", PresetContent )
		PresetContentName:SetFont("HUDText_Vendor")
		PresetContentName:SetText( Char_Presets[index][1] )
		PresetContentName:SetTextColor( Color(255, 255, 255, 235) )
		PresetContentName:CenterHorizontal()
		PresetContentName:SizeToContents()
		PresetContentName:SetPos( (212-(PresetContentName:GetSize()/2)+16), 5 )
	
	PresetContentClass = vgui.Create( "DLabel", PresetContent )
		PresetContentClass:SetFont("HUDText_BloodySlime")
		PresetContentClass:SetText( Char_Presets[index][2] )
		PresetContentClass:SetTextColor( Color(255, 255, 25, 235) )
		PresetContentClass:CenterHorizontal()
		PresetContentClass:SizeToContents()
		PresetContentClass:SetPos( (212-(PresetContentClass:GetSize()/2)+16), 30 )
		
	PresetContentClassImage = vgui.Create("DImage", PresetNamePanel)
		PresetContentClassImage:SetPos( 0, 0 )
		PresetContentClassImage:SetSize( 50, 50 )
		PresetContentClassImage:SetImage(classicon)
		
	PresetContentClassImage = vgui.Create("DImage", PresetNamePanel)
		PresetContentClassImage:SetPos( 413, 0 )
		PresetContentClassImage:SetSize( 50, 50 )
		PresetContentClassImage:SetImage(classicon)
		
	PresetSpellPanel = vgui.Create("DPanel", PresetContent)
		PresetSpellPanel:SetPos( 0, 53 )
		PresetSpellPanel:SetSize( 463, 100 )
		PresetSpellPanel:SetBackgroundColor(Color(0, 0, 0, 140))

	PresetContentName = vgui.Create( "DLabel", PresetContent )
		PresetContentName:SetFont("HUDText_BloodySlime")
		PresetContentName:SetText( "Spells:" )
		PresetContentName:SetTextColor( Color(255, 255, 255, 235) )
		PresetContentName:CenterHorizontal()
		PresetContentName:SizeToContents()
		PresetContentName:SetPos( (212-(PresetContentName:GetSize()/2)+16), 53 )
		
	MakeSpellList(PresetSpellPanel, index)
	
	PresetRegenPanel = vgui.Create("DPanel", PresetContent)
		PresetRegenPanel:SetPos( 0, 156 )
		PresetRegenPanel:SetSize( 463, 300 )
		PresetRegenPanel:SetBackgroundColor(Color(0, 0, 0, 140))
		
	local PresetRegenName = vgui.Create( "DLabel", PresetRegenPanel )
		PresetRegenName:SetFont("HUDText_BloodySlime")
		PresetRegenName:SetText( "Resource Type:" )
		PresetRegenName:SetTextColor( Color(255, 255, 255, 235) )
		PresetRegenName:CenterHorizontal()
		PresetRegenName:SizeToContents()
		PresetRegenName:SetPos( (200-(PresetContentName:GetSize()/2)), 8 )
		
	MakeRegenList(PresetRegenPanel, index)
		
	PresetContentClassImage = vgui.Create("DImage", PresetNamePanel)
		PresetContentClassImage:SetPos( 413, 0 )
		PresetContentClassImage:SetSize( 50, 50 )
		PresetContentClassImage:SetImage(classicon)
		
	PresetGearPanel = vgui.Create("DPanel", PresetContent)
		PresetGearPanel:SetPos( 0,  460)
		PresetGearPanel:SetSize( 463, 204 )
		PresetGearPanel:SetBackgroundColor(Color(0, 0, 0, 140))
		
end

function MakeRegenList(parent, index)
	local regen_type = Char_Presets[index][6]
	
	local RegenList = vgui.Create( "DComboBox", parent )
		RegenList:SetPos( 175, 35 )
		RegenList:SetSize( 100, 20 )
		RegenList:SetValue( "Select one." )
		RegenList.OnSelect = function(panel,index2,value,data)
			surface.PlaySound("devin/blackbetty/menu/select_target.wav")
			Char_Presets[index][6] = index2
			net.Start("UpdatePlayerPresets")
			net.WriteString(util.TableToJSON(Char_Presets))
			net.SendToServer()
			DisplayRegenInfo(parent, index2)
		end
		for i = 1, #RegenTypes, 1 do
			RegenList:AddChoice(RegenTypes[i][1])
		end
	if(regen_type != nil)then
		RegenList:ChooseOptionID(regen_type)
	end
	
	local SpawnWithPreset = vgui.Create( "DButton", parent )
		local CurrentSpellIDs = {}
		SpawnWithPreset:SetPos( 300, 90 )
		SpawnWithPreset:SetText( "[TEMP]Spawn with Preset" )
		SpawnWithPreset:SetSize( 128, 20 )
		for k, v in pairs(CurrentSetSpells)do
			table.insert(CurrentSpellIDs, CurrentSetSpells[k][6])
		end
		for i = 1, 5, 1 do
			if(CurrentSpellIDs[i] == nil)then
				CurrentSpellIDs[i] = -10
			end
		end
		SpawnWithPreset.DoClick = function()
			local preset_info = {}
			preset_info[1] = CurrentSpellIDs
			preset_info[2] = Char_Presets[index][6]
			preset_info[3] = CurrentClassName
			-- for i = 1, #ClassFillers, 1 do
				-- if(CurrentClassName == ClassFillers[i][1])then
					-- table.insert(preset_info[1], ClassFillers[i][2])
					-- table.insert(preset_info[1], ClassFillers[i][3])
				-- end
			-- end
			net.Start("SpawnWithPreset")
			net.WriteTable(preset_info)
			net.SendToServer()
			optionsMenu:Close()
		end
end

function DisplayRegenInfo(parent, index)
	if(RegenInfoPanel != nil)then
		RegenInfoPanel:Remove()
	end
	RegenInfoPanel = vgui.Create( "DPanel", parent )
		RegenInfoPanel:SetPos( 0, 65 )
		RegenInfoPanel:SetSize( 463, 235 )
		RegenInfoPanel:SetBackgroundColor(Color(0, 0, 0, 0))
		
	RegenInfoImage = vgui.Create( "DImage", RegenInfoPanel )
		RegenInfoImage:SetPos(193, 0)
		RegenInfoImage:SetSize( 64, 64 )
		RegenInfoImage:SetImage(RegenTypes[index][3])
		
	RegenInfoImage = vgui.Create( "DLabel", RegenInfoPanel)
		RegenInfoImage:SetPos(8, 16)
		RegenInfoImage:SetSize( 463, 256 )
		RegenInfoImage:SetFont("HUDText_BloodySlime")
		RegenInfoImage:SetTextColor(Color(255, 255, 0))
		RegenInfoImage:SetText(RegenTypes[index][2])
		
end

function MakeSpellList(parent, index)
	local xoffset = 6
	SpellBoxes = {}
	
	if(SpellBox1 != nil)then
		SpellBox1:Remove()
	end
	
	local SpellBox1 = vgui.Create( "DImageButton", parent )
		SpellBox1:SetPos( 34+xoffset, 30 )
		SpellBox1:SetSize( 50, 50 )
		SpellBox1:SetImage("devin/blackbetty/hudicons/plus.png")
		SpellBox1.OnCursorEntered = function()
			SpellBox1:SetColor(Color(25, 255, 25, 255))
		end
		SpellBox1.OnCursorExited = function()
			SpellBox1:SetColor(Color(255, 255, 255, 255))
		end
	SpellBoxes[1] = SpellBox1
	
	if(SpellBox2 != nil)then
		SpellBox2:Remove()
	end
	
	local SpellBox2 = vgui.Create( "DImageButton", parent )
		SpellBox2:SetPos( 118+xoffset, 30 )
		SpellBox2:SetSize( 50, 50 )
		SpellBox2:SetImage("devin/blackbetty/hudicons/plus.png")
		SpellBox2.OnCursorEntered = function()
			SpellBox2:SetColor(Color(25, 255, 25, 255))
		end
		SpellBox2.OnCursorExited = function()
			SpellBox2:SetColor(Color(255, 255, 255, 255))
		end
	SpellBoxes[2] = SpellBox2
		
	if(SpellBox3 != nil)then
		SpellBox3:Remove()
	end
	
	local SpellBox3 = vgui.Create( "DImageButton", parent )
		SpellBox3:SetPos( 200+xoffset, 30 )
		SpellBox3:SetSize( 50, 50 )
		SpellBox3:SetImage("devin/blackbetty/hudicons/plus.png")
		SpellBox3.OnCursorEntered = function()
			SpellBox3:SetColor(Color(25, 255, 25, 255))
		end
		SpellBox3.OnCursorExited = function()
			SpellBox3:SetColor(Color(255, 255, 255, 255))
		end
	SpellBoxes[3] = SpellBox3
	
	if(SpellBox4 != nil)then
		SpellBox4:Remove()
	end
	
	local SpellBox4 = vgui.Create( "DImageButton", parent )
		SpellBox4:SetPos( 284+xoffset, 30 )
		SpellBox4:SetSize( 50, 50 )
		SpellBox4:SetImage("devin/blackbetty/hudicons/plus.png")
		SpellBox4.OnCursorEntered = function()
			SpellBox4:SetColor(Color(25, 255, 25, 255))
		end
		SpellBox4.OnCursorExited = function()
			SpellBox4:SetColor(Color(255, 255, 255, 255))
		end
	SpellBoxes[4] = SpellBox4
		
	if(SpellBox5 != nil)then
		SpellBox5:Remove()
	end
	
	local SpellBox5 = vgui.Create( "DImageButton", parent )
		SpellBox5:SetPos( 368+xoffset, 30 )
		SpellBox5:SetSize( 50, 50 )
		SpellBox5:SetImage("devin/blackbetty/hudicons/plus.png")
		SpellBox5.OnCursorEntered = function()
			SpellBox5:SetColor(Color(25, 255, 25, 255))
		end
		SpellBox5.OnCursorExited = function()
			SpellBox5:SetColor(Color(255, 255, 255, 255))
		end
	SpellBoxes[5] = SpellBox5
		
	if(Char_Presets[index][2] == "Man At Arms")then
		AllowedTypes = MAA_SpellTypes
	elseif(Char_Presets[index][2] == "Shieldbearer")then
		AllowedTypes = SB_SpellTypes
	elseif(Char_Presets[index][2] == "Brawler")then
		AllowedTypes = BRAWL_SpellTypes
	elseif(Char_Presets[index][2] == "Mage")then
		AllowedTypes = MAGE_SpellTypes
	elseif(Char_Presets[index][2] == "Blademaster")then
		AllowedTypes = BM_SpellTypes
	elseif(Char_Presets[index][2] == "Ranger")then
		AllowedTypes = RNG_SpellTypes
	end
	
	for a, z in pairs(UsableSpells)do
		for k, v in pairs(AllSpells) do
			for i, o in pairs(AllowedTypes)do
				if(AllSpells[k][8] == AllowedTypes[a])then
					table.insert(UsableSpells[a], AllSpells[k])
					break
				end
			end
		end
	end
	
	if(Char_Presets[index][3] == nil)then
		Char_Presets[index][3] = {}
	end
	
	for k, v in pairs(SpellBoxes) do
		SpellBoxes[k].OnMousePressed = function()
			CreateSwapMenu("spell", UsableSpells[k], k, PresetContent)
			surface.PlaySound("devin/blackbetty/menu/spellbook_open.wav")
		end
	end
	
	for k, v in pairs(SpellBoxes)do
		if(Char_Presets[index][3][k] == nil)then
			Char_Presets[index][3][k] = 0
			net.Start("UpdatePlayerPresets")
			net.WriteString(util.TableToJSON(Char_Presets))
			net.SendToServer()
		end
		if(Char_Presets[index][3][k] > 0)then
			for i, o in pairs(UsableSpells[k])do
				print("ass")
				if(UsableSpells[k][i][6] == Char_Presets[index][3][k])then
					SpellBoxes[k]:SetImage(UsableSpells[k][i][2])
					CurrentSetSpells[k] = UsableSpells[k][i]
					break
				end
			end
		end
	end
end

function CreateSwapMenu(swaptype, data, index, parent)
	local choosetext = swaptype
	
	local tooltipPanel = nil
	
	local FramePanel = vgui.Create("DPanel", parent)
		FramePanel:SetPos( 0, 0 )
		FramePanel:SetSize( 463, 765 )
		FramePanel:SetBackgroundColor(Color(155, 155, 155, 255))
	
	local ToolTipPanel1 = vgui.Create("DPanel", parent)
		ToolTipPanel1:SetPos( 5, 5 )
		ToolTipPanel1:SetSize( 453, 150 )
		ToolTipPanel1:SetBackgroundColor(Color(65, 65, 65, 255))
	
	if(choosetext == "spell" and CurrentSetSpells[index])then
	
		firsttooltipPanel = vgui.Create("DImage", ToolTipPanel1)
			firsttooltipPanel:SetVisible(true)
			firsttooltipPanel:SetImage("devin/blackbetty/tooltip/window.png")
			firsttooltipPanel:SetSize(165, 108)
			firsttooltipPanel:SetPos(16, 21)
		firsttooltipImage = vgui.Create("DImage", firsttooltipPanel)
			firsttooltipImage:SetVisible(true)
			firsttooltipImage:SetImage(CurrentSetSpells[index][2])
			firsttooltipImage:SetSize(48, 48)
			firsttooltipImage:SetPos(4, 4)
		firsttooltipTitle = vgui.Create("DLabel", firsttooltipPanel)
			firsttooltipTitle:SetVisible(true)
			firsttooltipTitle:SetText(CurrentSetSpells[index][1])
			firsttooltipTitle:SetSize(128, 16)
			firsttooltipTitle:SetPos(64, 8)
		firsttooltipTitle = vgui.Create("DLabel", firsttooltipPanel)
			firsttooltipTitle:SetVisible(true)
			firsttooltipTitle:SetText("Range: "..((CurrentSetSpells[index][4]/16)/3).." yds." )
			firsttooltipTitle:SetSize(128, 32)
			firsttooltipTitle:SetPos(64, 16)
		firsttooltipDesc = vgui.Create("DLabel", firsttooltipPanel)
			firsttooltipDesc:SetVisible(true)
			firsttooltipDesc:SetText(CurrentSetSpells[index][3])
			firsttooltipDesc:SetSize(256, 64)
			firsttooltipDesc:SetPos(12, 48)
	else
		Empty = vgui.Create( "DImageButton", parent )
			Empty:SetPos( 85, 50 )
			Empty:SetSize( 50, 50 )
			Empty:SetImage("devin/blackbetty/hudicons/plus.png")
			
		ChooseItemTextNone = vgui.Create( "DLabel",  parent )
			ChooseItemTextNone:SetFont("HUDText_Vendor")
			ChooseItemTextNone:SetText( "None" )
			ChooseItemTextNone:SetPos(85, 105)
			ChooseItemTextNone:SizeToContents()
			ChooseItemTextNone:SetTextColor( Color(255, 25, 25, 235) )
	end
	hovertooltipPanel = vgui.Create("DImage", ToolTipPanel1)
		hovertooltipPanel:SetVisible(true)
		hovertooltipPanel:SetImage("devin/blackbetty/tooltip/window.png")
		hovertooltipPanel:SetSize(165, 108)
		hovertooltipPanel:SetPos(273, 21)
		
	local ToolTipPanel = vgui.Create("DPanel", parent)
		ToolTipPanel:SetPos( 0, 160 )
		ToolTipPanel:SetSize( 463, 605 )
		ToolTipPanel:SetBackgroundColor(Color(65, 65, 65, 255))
		
	local ChooseItemTextTo = vgui.Create( "DLabel",  parent )
		ChooseItemTextTo:SetFont("HUDText_Vendor")
		ChooseItemTextTo:SetText( "To" )
		ChooseItemTextTo:SetPos(217, 75)
		ChooseItemTextTo:SizeToContents()
		ChooseItemTextTo:SetTextColor( Color(255, 255, 25, 235) )
		
	local Scroll = vgui.Create( "DScrollPanel", ToolTipPanel ) 
	Scroll:SetSize( 447, 605 )
	Scroll:SetPos( 16, 16 )

	local List   = vgui.Create( "DIconLayout", Scroll )
	List:SetSize( 447, 605 )
	List:SetPos( 0, 0 )
	List:SetSpaceY( 5 ) //Sets the space inbetween the panels on the X Axis by 5
	List:SetSpaceX( 5 ) //Sets the space inbetween the panels on the Y Axis by 5

	for i = 1, #data do
		local ListItem = List:Add( "DImageButton" )
		ListItem:SetSize( 64, 64 )
		ListItem:SetImage( data[i][2])
		ListItem.OnCursorEntered = function()
			if(hovertooltipPanel != nil)then
				hovertooltipPanel:Remove()
			end
			
			hovertooltipPanel = vgui.Create("DImage", ToolTipPanel1)
				hovertooltipPanel:SetVisible(true)
				hovertooltipPanel:SetImage("devin/blackbetty/tooltip/window.png")
				hovertooltipPanel:SetSize(165, 108)
				hovertooltipPanel:SetPos(273, 21)
			hovertooltipImage = vgui.Create("DImage", hovertooltipPanel)
				hovertooltipImage:SetVisible(true)
				hovertooltipImage:SetImage(UsableSpells[index][i][2])
				hovertooltipImage:SetSize(48, 48)
				hovertooltipImage:SetPos(4, 4)
			hovertooltipTitle = vgui.Create("DLabel", hovertooltipPanel)
				hovertooltipTitle:SetVisible(true)
				hovertooltipTitle:SetText(UsableSpells[index][i][1])
				hovertooltipTitle:SetSize(128, 16)
				hovertooltipTitle:SetPos(64, 8)
			hovertooltipTitle = vgui.Create("DLabel", hovertooltipPanel)
				hovertooltipTitle:SetVisible(true)
				hovertooltipTitle:SetText("Range: "..((UsableSpells[index][i][4]/16)/3).." yds." )
				hovertooltipTitle:SetSize(128, 32)
				hovertooltipTitle:SetPos(64, 16)
			hovertooltipDesc = vgui.Create("DLabel", hovertooltipPanel)
				hovertooltipDesc:SetVisible(true)
				hovertooltipDesc:SetText(UsableSpells[index][i][3])
				hovertooltipDesc:SetSize(256, 64)
				hovertooltipDesc:SetPos(12, 48)
			
			if(!ListItem.Selected)then
				ListItem:SetColor(Color(0, 155, 0, 255))
			end
		end
		ListItem.OnCursorExited = function()
			if(hovertooltipPanel != nil)then
				hovertooltipPanel:Remove()
			end
			if(!ListItem.Selected)then
				ListItem:SetColor(Color(255, 255, 255, 255))
			end
		end
		ListItem.DoClick = function()
			if(ListItem.Selected == nil)then
				ListItem.Selected = false
			end
			if(ListItem.Selected)then
				if(selectedtooltipPanel != nil)then
					selectedtooltipPanel:Remove()
					hovertooltipPanel = vgui.Create("DImage", ToolTipPanel1)
					hovertooltipPanel:SetVisible(true)
					hovertooltipPanel:SetImage("devin/blackbetty/tooltip/window.png")
					hovertooltipPanel:SetSize(165, 108)
					hovertooltipPanel:SetPos(273, 21)
				end
				surface.PlaySound("devin/blackbetty/menu/spell_deselect.wav")
				ListItem:SetColor(Color(255, 255, 255, 255))
				ListItem.Selected = false
			else
				for x, z in pairs(List:GetChildren())do
					if(z.Selected)then
						z.Selected = false
						z:SetColor(Color(255, 255, 255, 255))
						SpellToSwap = 0
					end
				end
				ListItem.Selected = true
				if(hovertooltipPanel != nil)then
					hovertooltipPanel:Remove()
				end
				if(selectedtooltipPanel != nil)then
					selectedtooltipPanel:Remove()
				end
				selectedtooltipPanel = vgui.Create("DImage", ToolTipPanel1)
					selectedtooltipPanel:SetVisible(true)
					selectedtooltipPanel:SetImage("devin/blackbetty/tooltip/window.png")
					selectedtooltipPanel:SetSize(165, 108)
					selectedtooltipPanel:SetPos(273, 21)
				selectedtooltipImage = vgui.Create("DImage", selectedtooltipPanel)
					selectedtooltipImage:SetVisible(true)
					selectedtooltipImage:SetImage(UsableSpells[index][i][2])
					selectedtooltipImage:SetSize(48, 48)
					selectedtooltipImage:SetPos(4, 4)
				selectedtooltipTitle = vgui.Create("DLabel", selectedtooltipPanel)
					selectedtooltipTitle:SetVisible(true)
					selectedtooltipTitle:SetText(UsableSpells[index][i][1])
					selectedtooltipTitle:SetSize(128, 16)
					selectedtooltipTitle:SetPos(64, 8)
				selectedtooltipTitle = vgui.Create("DLabel", selectedtooltipPanel)
					selectedtooltipTitle:SetVisible(true)
					selectedtooltipTitle:SetText("Range: "..((UsableSpells[index][i][4]/16)/3).." yds." )
					selectedtooltipTitle:SetSize(128, 32)
					selectedtooltipTitle:SetPos(64, 16)
				selectedtooltipDesc = vgui.Create("DLabel", selectedtooltipPanel)
					selectedtooltipDesc:SetVisible(true)
					selectedtooltipDesc:SetText(UsableSpells[index][i][3])
					selectedtooltipDesc:SetSize(256, 64)
					selectedtooltipDesc:SetPos(12, 48)
				surface.PlaySound("devin/blackbetty/menu/spell_select.wav")
				ListItem:SetColor(Color(255, 25, 25, 255))
				SpellToSwap = UsableSpells[index][i][6]
			end
		end
		
		ListItem.Think = function()
			if(ClearSelected)then
				ListItem:SetColor(Color(255, 255, 255, 255))
				ListItem.Selected = false
			end
		end
	end
		
	local ChooseItemText = vgui.Create( "DLabel", parent )
		ChooseItemText:SetPos( 30, 160 )
		ChooseItemText:SetSize( 245, 200 )
		ChooseItemText:SetText( "Replace your selection with an item below." )
		ChooseItemText:CenterHorizontal()
		ChooseItemText:SizeToContentsY()
	
	local ConfirmButton = vgui.Create( "DButton", parent )
			ConfirmButton:SetPos( 350, 615 )
			ConfirmButton:SetText( "Change Spell" )
			ConfirmButton:SetSize( 100, 40 )
			ConfirmButton.DoClick = function()
				if(SpellToSwap > 0)then
					print(SelectedPreset)
					print(index)
					print(SpellToSwap)
					Char_Presets[SelectedPreset][3][index] = SpellToSwap
					net.Start("UpdatePlayerPresets")
					net.WriteString(util.TableToJSON(Char_Presets))
					net.SendToServer()
					surface.PlaySound("devin/blackbetty/menu/spell_deselect.wav")
					FramePanel:Remove()
					ToolTipPanel1:Remove()
					ToolTipPanel:Remove()
					ChooseItemTextTo:Remove()					
					if(firsttooltipPanel!= nil)then
						firsttooltipPanel:Remove()
					end
					if(Empty!= nil)then
						Empty:Remove()
						ChooseItemTextNone:Remove()
					end
					ConfirmButton:Remove()
					UsableSpells = {{},{},{},{},{}}
					AllowedTypes = {}
					CurrentSetSpells = {}
					CurrentSetSpellImages = {}
					SpellBoxes = {}
					MakePresetContent(CharPresets, SelectedPreset)
				end
		end
	
	local CancelButton = vgui.Create( "DButton", parent )
		CancelButton:SetPos( 15, 615 )
		CancelButton:SetText( "Cancel" )
		CancelButton:SetSize( 100, 40 )
		CancelButton.DoClick = function()
			surface.PlaySound("devin/blackbetty/menu/spellbook_close.wav")
			FramePanel:Remove()
			ToolTipPanel1:Remove()
			ToolTipPanel:Remove()
			ChooseItemTextTo:Remove()
			if(firsttooltipPanel!= nil)then
				firsttooltipPanel:Remove()
			end
			if(Empty!= nil)then
				Empty:Remove()
				ChooseItemTextNone:Remove()
			end
			ConfirmButton:Remove()
			CancelButton:Remove()
		end	
end

function RemovePresetContent()
	if(PresetContent != nil)then
		PresetContent:Remove()
	end
end

function VGUI_PresetThink()
	if(RemovePreset != nil and MenuOpen)then
		if(RemovePreset != nil and #Char_Presets <= 0 and RemovePreset:IsVisible())then
			print(#Char_Presets)
			RemovePreset:SetVisible(false)
			RemovePresetContent()
		elseif(RemovePreset != nil and #Char_Presets > 0 and !RemovePreset:IsVisible())then
			--RemovePreset:SetVisible(true)
		end
	end
end
hook.Add("Think", "PresetThink", VGUI_PresetThink)
hook.Add("HUDPaint", "DrawMapHUD", HUD_DrawMapHUD)